/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetlif13.view;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import projetlif13.model.Skin;
import projetlif13.scene.GameScene;
import projetlif13.tools.PreferencesBundler;

/**
 *
 * @author yohan
 */
public class SkinView extends javax.swing.JFrame {

    private GameView gv;
    private int skinpawnselected;
    private int skinboardselected;
    private JFileChooser chooser = new JFileChooser(new File("."));

    /**
     * Creates new form SkinView
     */
    public SkinView(GameView g) {
        initComponents();
        gv = g;
        addWindowListener(new WindowAdapter() {
            //On aurait très bien pu le faire avec du modal...

            @Override
            public void windowClosing(WindowEvent e) {
                gv.setEnabled(true);
                dispose();
                GameScene.uiclick.play();
            }
        });
        skinpawnselected = GameScene.currentpawnskin.getType();
        skinboardselected = GameScene.currentboardskin.getType();
        //set les boutons en fonction du current dans le gamescene
        switch (skinpawnselected) {
            case Skin.SKIN_FLOWER:
                toggleflower.setSelected(true);
                togglepawn.setSelected(false);
                toggleplanet.setSelected(false);
                break;
            case Skin.SKIN_PAWN:
                toggleflower.setSelected(false);
                togglepawn.setSelected(true);
                toggleplanet.setSelected(false);
                break;
            case Skin.SKIN_PLANETE:
                toggleflower.setSelected(false);
                togglepawn.setSelected(false);
                toggleplanet.setSelected(true);
                break;
        }
        switch (skinboardselected) {
            case Skin.SKIN_FLOWER:
                slotbasetoggle.setSelected(false);
                slotboistoggle.setSelected(true);
                slotpsychotoggle.setSelected(false);
                break;
            case Skin.SKIN_PAWN:
                slotbasetoggle.setSelected(true);
                slotboistoggle.setSelected(false);
                slotpsychotoggle.setSelected(false);
                break;
            case Skin.SKIN_PLANETE:
                slotbasetoggle.setSelected(false);
                slotboistoggle.setSelected(false);
                slotpsychotoggle.setSelected(true);
                break;
        }
        /*
         * Charger le background (label only)
         */
        if (GameScene.backgroundpath != null) {
            jLabel4.setText("..." + GameScene.backgroundpath.substring(GameScene.backgroundpath.length() - (GameScene.backgroundpath.length() / 2)));
            jLabel4.setEnabled(true);
        } else {
            jLabel4.setText("Aucun");
            jLabel4.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        togglepawn = new javax.swing.JToggleButton();
        toggleflower = new javax.swing.JToggleButton();
        toggleplanet = new javax.swing.JToggleButton();
        slotbasetoggle = new javax.swing.JToggleButton();
        slotboistoggle = new javax.swing.JToggleButton();
        slotpsychotoggle = new javax.swing.JToggleButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Choix de l'apparence");
        setResizable(false);

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Annuler");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel1.setText("Style de jeu :");

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel2.setText("Plateau de jeu :");

        togglepawn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/pionoverview.png"))); // NOI18N
        togglepawn.setLabel("");
        togglepawn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                togglepawnActionPerformed(evt);
            }
        });

        toggleflower.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/fleuroverview.png"))); // NOI18N
        toggleflower.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleflowerActionPerformed(evt);
            }
        });

        toggleplanet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/planeteoverview.png"))); // NOI18N
        toggleplanet.setLabel("");
        toggleplanet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleplanetActionPerformed(evt);
            }
        });

        slotbasetoggle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/slotoverview.png"))); // NOI18N
        slotbasetoggle.setLabel("");
        slotbasetoggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slotbasetoggleActionPerformed(evt);
            }
        });

        slotboistoggle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/slotoverview_bois.png"))); // NOI18N
        slotboistoggle.setLabel("");
        slotboistoggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slotboistoggleActionPerformed(evt);
            }
        });

        slotpsychotoggle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetlif13/gfx/slotoverview_psycho.png"))); // NOI18N
        slotpsychotoggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slotpsychotoggleActionPerformed(evt);
            }
        });

        jButton3.setText("Parcourir...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel3.setText("Arrière plan personnalisé :");

        jLabel4.setText("Aucun");
        jLabel4.setEnabled(false);

        jButton4.setText("Effacer");
        jButton4.setToolTipText("Enlève le fond d'écran du jeu");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(togglepawn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(toggleflower, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(toggleplanet, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(slotbasetoggle, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(slotboistoggle, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(slotpsychotoggle, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(69, 69, 69)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(togglepawn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(toggleflower, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(toggleplanet, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(slotbasetoggle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(slotboistoggle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(slotpsychotoggle, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        gv.setEnabled(true);
        dispose();
        GameScene.uiclick.play();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        GameScene.changeSkin(skinpawnselected, skinboardselected);
        /*
         * On save les props
         */
        PreferencesBundler.putValue("skin.board", skinboardselected);
        PreferencesBundler.putValue("skin.pawn", skinpawnselected);
        jButton2ActionPerformed(null);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void togglepawnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_togglepawnActionPerformed
        // TODO add your handling code here:
        //skin pawn
        skinpawnselected = Skin.SKIN_PAWN;
        togglepawn.setSelected(true);
        toggleflower.setSelected(false);
        toggleplanet.setSelected(false);
        GameScene.uiclick.play();
    }//GEN-LAST:event_togglepawnActionPerformed

    private void toggleflowerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleflowerActionPerformed
        // TODO add your handling code here:
        skinpawnselected = Skin.SKIN_FLOWER;
        togglepawn.setSelected(false);
        toggleflower.setSelected(true);
        toggleplanet.setSelected(false);
        GameScene.uiclick.play();
    }//GEN-LAST:event_toggleflowerActionPerformed

    private void toggleplanetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleplanetActionPerformed
        skinpawnselected = Skin.SKIN_PLANETE;
        togglepawn.setSelected(false);
        toggleflower.setSelected(false);
        toggleplanet.setSelected(true);
        GameScene.uiclick.play();
    }//GEN-LAST:event_toggleplanetActionPerformed

    private void slotbasetoggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slotbasetoggleActionPerformed
        skinboardselected = Skin.SKIN_PAWN;
        slotbasetoggle.setSelected(true);
        slotboistoggle.setSelected(false);
        slotpsychotoggle.setSelected(false);
        GameScene.uiclick.play();
    }//GEN-LAST:event_slotbasetoggleActionPerformed

    private void slotboistoggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slotboistoggleActionPerformed
        skinboardselected = Skin.SKIN_FLOWER;
        slotbasetoggle.setSelected(false);
        slotboistoggle.setSelected(true);
        slotpsychotoggle.setSelected(false);
        GameScene.uiclick.play();
    }//GEN-LAST:event_slotboistoggleActionPerformed

    private void slotpsychotoggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slotpsychotoggleActionPerformed
        skinboardselected = Skin.SKIN_PLANETE;
        slotbasetoggle.setSelected(false);
        slotboistoggle.setSelected(false);
        slotpsychotoggle.setSelected(true);
        GameScene.uiclick.play();
    }//GEN-LAST:event_slotpsychotoggleActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //Open a custom background for morpion p4 etc..
        chooser.setDialogTitle("Open image (.jpg,.png,.gif");
        int resp = chooser.showOpenDialog(this);
        if (resp == JFileChooser.APPROVE_OPTION) {

            File image = chooser.getSelectedFile();
            PreferencesBundler.putValue("skin.background", image.getAbsolutePath());
            jLabel4.setText(image.getName());
            jLabel4.setEnabled(true);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(image);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(SkinView.class.getName()).log(Level.SEVERE, null, ex);
            }
            image.setReadable(true, false);
            image.setWritable(true, false);
            image.setExecutable(true, false);
            Image imagebuff = null;
            Graphics g;
            try {
                imagebuff = new Image(512, 512);
                g = imagebuff.getGraphics();
                Image img = new Image(fis, image.getAbsolutePath(), false);
                g.drawImage(img, 0, 0);
                g.drawImage(img, img.getWidth(), 0);
                g.drawImage(img, img.getWidth(), img.getHeight());
                g.drawImage(img, 0, img.getHeight());
                g.flush();
                img.destroy();

            } catch (SlickException ex) {
                Logger.getLogger(SkinView.class.getName()).log(Level.SEVERE, null, ex);
            }

            GameScene.background = imagebuff;

            try {
                fis.close();
                //img.destroy();
                //img.destroy();
            } catch (IOException ex) {
                Logger.getLogger(SkinView.class.getName()).log(Level.SEVERE, null, ex);
            }



        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        GameScene.background = null;
        PreferencesBundler.putValue("skin.background", "");
        jLabel4.setText("Aucun");
        jLabel4.setEnabled(false);
    }//GEN-LAST:event_jButton4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JToggleButton slotbasetoggle;
    private javax.swing.JToggleButton slotboistoggle;
    private javax.swing.JToggleButton slotpsychotoggle;
    private javax.swing.JToggleButton toggleflower;
    private javax.swing.JToggleButton togglepawn;
    private javax.swing.JToggleButton toggleplanet;
    // End of variables declaration//GEN-END:variables
}
